﻿using System;
using System.Threading;
using EasyNetQ;
using ZLWeb.WebApiEntityBase;

namespace ZL.Web.Utility.MQ
{
    public class MQHelper
    {

        ///// <summary>
        ///// 发送消息
        ///// </summary>
        //public static void Publish<T>(T msg) where T: QMessage
        //{
        //    //// 创建消息bus
        //    IBus bus = BusBuilder.CreateMessageBus();
        //    try
        //    {
        //        bus.Publish<T>(msg, msg.MessageRouter);
        //    }
        //    catch (EasyNetQException ex)
        //    {
        //        //处理连接消息服务器异常 
        //    }

        //    bus.Dispose();//与数据库connection类似，使用后记得销毁bus对象
        //}


        /// <summary>
        /// 接收消息
        /// </summary>
        /// <param name="msg"></param>
        public static void Subscribe<T>(T msg, IMQMessage ipro) where T : QMessage
        {
            //// 创建消息bus
            IBus bus = BusBuilder.CreateMessageBus();

            try
            {
                bus.Subscribe<T>(msg.MessageRouter, message => ipro.SendProcessMsg(message), x => x.WithAutoDelete(false));

            }
            catch (EasyNetQException ex)
            {
                //处理连接消息服务器异常 
            }
        }

        /// <summary>
        /// 接收消息
        /// </summary>
        /// <param name="msg"></param>
        public static void ReturnSubscribe<T>(T msg, IWSProcessMessage ipro) where T : QMessage
        {
            //// 创建消息bus
            IBus bus = BusBuilder.CreateMessageBus();

            try
            {
                bus.Subscribe<T>(msg.MessageRouter, message => ipro.ReturnProcessMsg(message), x =>
                {
                    x.WithAutoDelete(true);
                   
                });

                int i = 0;
                while (!ipro.Ack && i<=1200)
                {
                    i++;
                    Thread.Sleep(5);
                    //IBus.Subscribe<T>(string, System.Action<T>, System.Action<EasyNetQ.FluentConfiguration.ISubscriptionConfiguration>)
                }
                if (i >= 1200)
                {
                   throw new Exception("消息队列等待超时:"+ msg.MessageRouter);
                }
            }
            catch (EasyNetQException ex)
            {
                //处理连接消息服务器异常 
            }
        }





        /// <summary>
        /// 发送消息
        /// </summary>
        public static void Send<T>(T msg) where T : QMessage
        {
            //// 创建消息bus
            IBus bus = BusBuilder.CreateMessageBus();
            try
            {
                bus.Send<T>(msg.Queue, msg);
                //bus.Send<T>(msg, msg.MessageRouter);
            }
            catch (EasyNetQException ex)
            {
                //处理连接消息服务器异常 
            }

            bus.Dispose();//与数据库connection类似，使用后记得销毁bus对象
        }


        /// <summary>
        /// 接收消息
        /// </summary>
        /// <param name="msg"></param>
        public static void Receive<T>(T msg, IMQMessage ipro) where T : QMessage
        {
            //// 创建消息bus
            IBus bus = BusBuilder.CreateMessageBus();

            try
            {
                bus.Receive<T>(msg.Queue, message => ipro.SendProcessMsg(message));

            }
            catch (EasyNetQException ex)
            {
                //处理连接消息服务器异常 
            }
        }

    }
}
